/*
 * hkh-project
 * leafteamwork.blogspot.com
 * Tool: Netbeans 6.7.1 - SQL Server 2005
 */

package vn.com.hkh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vn.com.hkh.dto.ContactDTO;
import vn.com.hkh.service.ContactService;

/**
 *
 * @author leaf
 */
public class ContactAction extends HttpServlet {

    ContactService cs = null;

    @Override
    public void init() throws ServletException {
        super.init();
        cs = new ContactService();
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String _forward = "";
        String _message = "";
        try {
            if (request.getSession().getAttribute("login") == null) {
                _message = "Please login!";
                request.setAttribute("message", _message);
                RequestDispatcher rd = request.getRequestDispatcher("/LoginAction");
                rd.forward(request, response);
            } else {
                // T01. CLIENT PROCESS
                String _action = request.getParameter("act");
                _action = (_action == null) ? "list" : _action;

                if (_action.equalsIgnoreCase("view")) {
                    String _primaryKey = request.getParameter("pk");
                   ContactDTO _contactDTO = cs.findByPrimarykey(_primaryKey);
                   request.setAttribute("contact", _contactDTO);
                   _forward = "/view/contactdetail.jsp";                   
                }else if (_action.equalsIgnoreCase("del")) {
                    String _primaryKey = request.getParameter("pk");
                    boolean result = cs.remove(_primaryKey);
                    _message = result ? "Delete Successful!" : "Delete Error!";
                    _action = "list";
                } else if (_action.equalsIgnoreCase("cancel")) {
                    _message = null;
                    _action = "list";
                }

                if (_action.equalsIgnoreCase("list")) {
                    Collection<ContactDTO> _contactDTO = cs.findContacts();
                    request.setAttribute("contacts", _contactDTO);
                    _forward = (_contactDTO == null) ? "" : "/view/contactlist.jsp";
                }

                //T04. LOOKUP & FORWARD TO VIEW
                request.setAttribute("from", "contactaction");
                request.setAttribute("message", _message);
                request.setAttribute("forward", _forward);
                RequestDispatcher rd = request.getRequestDispatcher("/view/adminlayout.jsp");
                rd.forward(request, response);

            }


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    @Override
    public void destroy() {
        super.destroy();
        cs = null;
    }
}
